import { ref, Ref } from 'vue'
import Project from '../types/Project';
import { BasicInfoViewModel } from "../types/ProjBasicInfo";
import dayjs, { Dayjs } from 'dayjs';
import { axios } from '@/axios';

export async function useBasicInfo(proj: Project){
    function reset () {
        basicInfo.value = proj.toBasicInfoViewModel();
    }
    const basicInfo: Ref<BasicInfoViewModel> = ref(new BasicInfoViewModel());
    const errorStrings: Ref<string[]> = ref([]);
    reset();

    const graduateTimeMsg = await axios.post('/api/proj/common/GetGraduateTime', {
        id: proj.basicInfo.ID
    }) as { success: boolean, isJwc: boolean, graduate: string};
    const disabledEndDate = (current: Dayjs) => {
        if (graduateTimeMsg.success) {
            if (graduateTimeMsg.isJwc) {
                return current && current < dayjs(basicInfo.value.applyStartDate);
            } else {
                return current && (current < dayjs(basicInfo.value.applyStartDate).add(2, 'year') || current > dayjs(graduateTimeMsg.graduate));
            }
        } else {
            return true;
        }
    }

    async function validate() {
        errorStrings.value = [];
        const info = basicInfo.value;

        // 必填内容（基本信息）：项目名称、所属学科、项目来源、课题来源、负责学院、所属学院、项目开始时间、项目结束时间、在实验/实践中完成（实验室类型、实验室名称、拟进行实践的地点）
        if (info.projectName === '') {
            errorStrings.value.push('项目名称不能为空');
            return false;
        }
        if (info.speciality === '') {
            errorStrings.value.push('所属学科不能为空');
            return false;
        }
        if (info.projectSource === '') {
            errorStrings.value.push('项目来源不能为空');
            return false;
        }
        if (info.problemSource === '') {
            errorStrings.value.push('课题来源不能为空');
            return false;
        }
        if (info.departId === '') {
            errorStrings.value.push('负责学院不能为空');
            return false;
        }
        if (info.belongDepartId === '') {
            errorStrings.value.push('所属学院不能为空');
            return false;
        }
        if (info.applyStartDate === '') {
            errorStrings.value.push('项目开始时间不能为空');
            return false;
        }
        if (info.applyEndDate === '') {
            errorStrings.value.push('项目结束时间不能为空');
            return false;
        }
        if (info.completeMethod !== '实验' && info.completeMethod !== '实践' && info.completeMethod !== '否') {
            errorStrings.value.push('在实验、实践中完成不能为空');
            return false;
        }
        if (info.completeMethod === '实验') {
            if (info.labType === '') {
                errorStrings.value.push('实验室类型不能为空');
                return false;
            }
            if (info.labName === '') {
                errorStrings.value.push('实验室名称不能为空');
                return false;
            }
        }
        if (info.completeMethod === '实践') {
            if (info.praticeLocation === '') {
                errorStrings.value.push('拟进行实践的地点不能为空');
                return false;
            }
        }

        // 时间检验
        if (dayjs(info.applyStartDate).isAfter(dayjs(info.applyEndDate))) {
            errorStrings.value.push('项目开始时间不能晚于项目结束时间: ' + info.applyEndDate);
            return false;
        }

        if (graduateTimeMsg.success && !graduateTimeMsg.isJwc) {
            if (dayjs(info.applyEndDate).isBefore(dayjs(info.applyStartDate).add(2, 'year'))) {
                errorStrings.value.push('项目结束时间不能早于项目开始时间的两年后：' + dayjs(info.applyStartDate).add(2, 'year').format('YYYY-MM-DD'));
                return false;
            }
            if (dayjs(info.applyEndDate).isAfter(dayjs(graduateTimeMsg.graduate))) {
                errorStrings.value.push('项目结束时间不能晚于毕业时间：' + graduateTimeMsg.graduate);
                return false;
            }
        }

        return true;
    }

    return {
        basicInfo,
        errorStrings,
        graduateTimeMsg,
        disabledEndDate,
        reset,
        validate
    }
}